# 假设我们已经有了多项式回归模型和预测函数
from sklearn.preprocessing import PolynomialFeatures
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

def polynomial_regression_sensitivity(years, pets, degrees):
    mse_list = []
    for degree in degrees:
        poly = PolynomialFeatures(degree)
        years_poly = poly.fit_transform(years)
        model = LinearRegression().fit(years_poly, pets)
        pets_pred = model.predict(years_poly)
        mse = np.mean((pets - pets_pred) ** 2)
        mse_list.append(mse)
    return mse_list

# 执行灵敏度分析
degrees = [1, 2, 3, 4, 5]
mse_cats = polynomial_regression_sensitivity(years, cats, degrees)
mse_dogs = polynomial_regression_sensitivity(years, dogs, degrees)

# 打印结果
print("Cats MSE for different polynomial degrees:", mse_cats)
print("Dogs MSE for different polynomial degrees:", mse_dogs)

# 可以进一步绘制MSE随度数变化的图表
plt.plot(degrees, mse_cats, label='Cats')
plt.plot(degrees, mse_dogs, label='Dogs')
plt.xlabel('Polynomial Degree')
plt.ylabel('Mean Squared Error')
plt.title('Sensitivity Analysis of Polynomial Degree')
plt.legend()
plt.show()